//
//  MyProfitTabel.swift
//  LeGou
//
//  Created by lxm on 2022/8/4.
//

import UIKit

class MyProfitTabel: XMBaseView {

    //MARK: --------------------属性--------------------
    var onClick : ((_ item : [String])->())?
    //MARK: --------------------UI控件--------------------
    let headerStack = ZMUtil.stackView(axis: .horizontal).then({$0.distribution = .fillEqually})
    
    let allStack = ZMUtil.stackView(axis: .vertical)
    
    var contentV : UIView?
    
    var data = [[String]]() {
        didSet {
            makeContent()
        }
    }
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        let titles = ["渠道", "付款笔数", "成交预估收益", "结算收益"]
        var i = 0
        for t in titles {
            let label = ZMUtil.label(title: t, color: UIColor.zm.color666666(), font: UIFont.systemFont(ofSize: 12))
            headerStack.addArrangedSubview(label)
            if i == 0 {
                label.textAlignment = .left
            } else if i == 3 {
                label.textAlignment = .center
            } else {
                label.textAlignment = .center
            }
            i += 1
        }
        allStack.spacing = 8
     
        addSubview(allStack)
        makeContent()
    }

    
    func makeContent() {
        contentV = nil
        allStack.arrangedSubviews.forEach { [weak allStack, weak self] v in
            guard let allStack = allStack, let _ = self else {return}
//            if let _ = v as? ProfitContentView {
                allStack.removeArrangedSubview(v)
                v.removeFromSuperview()
//            }
        }
        
        allStack.addArrangedSubview(headerStack)
        headerStack.snp.makeConstraints { make in
            make.height.equalTo(36).priority(.high)
            make.left.equalTo(allStack).offset(10)
            make.right.equalTo(allStack).offset(-6)
        }
        
//        layoutIfNeeded()
        var i = 0
        for item in data {
            let itemStack = ZMUtil.stackView(axis: .horizontal)
            itemStack.distribution = .fillEqually
            
//            var theLabels = [UILabel]()
            
            let title = ZMUtil.label(title: item[0], color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14, weight: .medium))
            title.adjustsFontSizeToFitWidth = true
            itemStack.addArrangedSubview(title)
          
            let num = ZMUtil.label(title: item[1], color: UIColor.zm.color666666(), font: UIFont.systemFont(ofSize: 12, weight: .regular))
            num.textAlignment = .center
            itemStack.addArrangedSubview(num)
            
            let price = ZMUtil.label(title: item[2], color: UIColor.zm.color666666(), font: UIFont.systemFont(ofSize: 12, weight: .regular))
            price.textAlignment = .center
            itemStack.addArrangedSubview(price)
            
            let lastV = UIView()
            
            let icon = ZMUtil.imageView(imageName: "right_temp")
            
            lastV.addSubview(icon)
            
            icon.snp.makeConstraints { make in
                make.right.equalToSuperview()
                make.centerY.equalToSuperview()
            }
            icon.tintColor = "#999999".zm.hex()
            
            
            let lastStack = ZMUtil.stackView(axis: .vertical)
            let settle = ZMUtil.label(title: item[3], color: UIColor.zm.color666666(), font: UIFont.systemFont(ofSize: 12, weight: .regular))
            settle.textAlignment = .right
            lastStack.addArrangedSubview(settle)
            
            let cant = ZMUtil.label(title: "(不可用)", color: Style.mainColor, font: UIFont.systemFont(ofSize: 12, weight: .regular))
            cant.textAlignment = .right
            lastStack.addArrangedSubview(cant)
            cant.isHidden = item[4] == "0"

            lastV.addSubview(lastStack)
            lastStack.snp.makeConstraints { make in
                make.top.bottom.equalToSuperview()
                make.left.greaterThanOrEqualTo(lastV).offset(0)
                make.right.equalTo(icon.snp.left).offset(0)
            }
            
            
            lastStack.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
            lastStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
            
            
            itemStack.addArrangedSubview(lastV)
            
            
            contentV = ProfitContentView()
            contentV?.backgroundColor = .red
            guard let contentV = contentV else {return}
            contentV.addSubview(itemStack)
            itemStack.snp.makeConstraints { make in
                make.bottom.top.equalToSuperview()
                make.left.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-6)
            }
            
            allStack.addArrangedSubview(contentV)
            contentV.snp.makeConstraints { make in
//                make.top.equalToSuperview().offset(4)
//                make.bottom.equalToSuperview().offset(-4)
                make.height.equalTo(36)
                make.right.left.equalToSuperview()
            }
            contentV.backgroundColor = i % 2 == 0 ? .white : "#F9F9F9".zm.hex()
            contentV.tag = i
            let tap = UITapGestureRecognizer()
            tap.addTarget(self, action: #selector(onTap(sender:)))
            contentV.addGestureRecognizer(tap)
            i += 1
            
            
            if ProfitIndexType.init(rawValue: Int(item[5]) ?? 0) ==  .hammer{
                let redNew = ZMUtil.imageView(imageName: "red_new")
                contentV.addSubview(redNew)
                redNew.snp.makeConstraints { make in
                    make.left.equalToSuperview().offset(55)
                    make.bottom.equalToSuperview().offset(-21)
                }
            }
        }
        
        layoutIfNeeded()
    }
    
    @objc func onTap(sender : UITapGestureRecognizer) {
        if let onClick = onClick {
            let item = data[sender.view?.tag ?? 0]
            onClick(item)
        }
    }
    
    func addConstraints() {
        allStack.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalToSuperview().offset(4)
            make.bottom.equalToSuperview().offset(-4)
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    //MARK: --------------------delegate--------------------

}

class ProfitContentView : UIView {
    
}
